//
//  TableViewController.m
//  musicPlayer
//
//  Created by sxiaojian on 14-9-9.
//  Copyright (c) 2014年 sxiaojian. All rights reserved.
//

#import "TableViewController.h"
#import "SDWebImage/UIImageView+WebCache.h"

#import "AFNetworking.h"
#import "MusicPlayerController.h"
#define identifier @"identifier"
@interface TableViewController ()
{
    NSMutableData *receivedData;
    NSXMLParser *chatParser;
}
@property (nonatomic,strong) NSArray *dataArray;
@end

@implementation TableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.url = @"http://tingapi.ting.baidu.com//v1/restserver/ting?from=android&version=2.4.0&method=baidu.ting.artist.getSongList&format=json&order=2&tinguid=1152&offset=0&limits=20";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];

    [manager GET:self.url parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.dataArray = responseObject[@"songlist"];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);

    }];

//    NSDictionary *info = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return self.dataArray.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 101;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    NSDictionary *dic = self.dataArray[indexPath.row];
    UIImageView *imageview = (UIImageView *)[cell viewWithTag:1];
    UILabel *label1 = (UILabel *)[cell viewWithTag:2];
    UILabel *label2 = (UILabel *)[cell viewWithTag:3];
    UILabel *label3 = (UILabel *)[cell viewWithTag:4];
    label1.text = dic[@"title"];
    int time = [dic[@"file_duration"] intValue];
    label2.text  = [NSString stringWithFormat:@"%@",[self TimeformatFromSeconds:time]];
    label3.text = [NSString stringWithFormat:@"%@*%@",dic[@"album_title"],dic[@"author"]];
    [imageview sd_setImageWithURL:[NSURL URLWithString:dic[@"pic_big"]]];
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    MusicPlayerController *player = [MusicPlayerController shareMusicViewController];

    NSDictionary *dic = self.dataArray[indexPath.row];
//    player.songUrl =
    NSString* url =  [NSString stringWithFormat:@"http://ting.baidu.com//data/music/links?songIds=%@",dic[@"song_id"]];
    player.IRCUrl = dic[@"lrclink"];
    player.originalDic = dic;
    [self.navigationController pushViewController:player animated:YES];

    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request
                                               returningResponse:nil error:nil];
    NSDictionary *returnDic = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableLeaves error:nil];
    NSDictionary *dic0 = returnDic[@"data"];
    NSArray *array = dic0[@"songList"];
    NSDictionary *dic1 = array[0];
    NSString *string = dic1[@"songLink"];
    player.songUrl = string;
//    player.songUrl = [[returnDic[@"data"] objectForKey:@"songList"] objectForKey:@"songLink"];
    
//    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
//    if (connection) {
//        
//    }
}
-(NSString*)TimeformatFromSeconds:(int)seconds
{
    int totalm = seconds/(60);
    int h = totalm/(60);
    int m = totalm%(60);
    int s = seconds%(60);
    if (h==0) {
        return  [[NSString stringWithFormat:@"%02d:%02d", m, s] substringToIndex:5];
    }
    return [NSString stringWithFormat:@"%02d:%02d:%02d", h, m, s];
}



@end
